package com.dubbo.lian.common.util;

import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Iterator;

/**
 * Created by Administrator on 2016/12/26 0026.
 */
public class ImageUtil {


    public static void saveUploadImageAndThumbnail(HttpServletRequest request, String path, String fileName) throws IOException {
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext());


        if(multipartResolver.isMultipart(request))
        {
            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;

            Iterator iter=multiRequest.getFileNames();
            File fileMkdir =new File(path);

            if  (!fileMkdir .exists()  && !fileMkdir .isDirectory())
            {
                fileMkdir.mkdirs();
            }
            while(iter.hasNext())
            {

                MultipartFile file=multiRequest.getFile(iter.next().toString());

                if(file!=null)
                {
                    String filePath=path+fileName;
                    String FullImage = filePath+".jpg";
                    String ThumbnailImage = filePath+"_t.jpg";

                    file.transferTo(new File(FullImage));


                    Thumbnails.of(file.getInputStream())
                            .scale(0.1)
                            .toFile(ThumbnailImage);
                }
            }
        }
    }



    public static void saveUploadImageAndThumbnail(InputStream file,String path, String fileName) throws IOException {


            File fileMkdir =new File(path);

            if  (!fileMkdir.exists()  && !fileMkdir.isDirectory())
            {
                fileMkdir.mkdirs();
            }

            if(file!=null)
            {
                String filePath=path+fileName;
                String FullImage = filePath+".jpg";
                String ThumbnailImage = filePath+"_t.jpg";

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while ((len = file.read(buffer)) > -1 ) {
                    baos.write(buffer, 0, len);
                }
                baos.flush();

                InputStream stream1 = new ByteArrayInputStream(baos.toByteArray());
                InputStream stream2 = new ByteArrayInputStream(baos.toByteArray());

                Thumbnails.of(stream1)
                        .scale(1)
                        .toFile(FullImage);

                Thumbnails.of(stream2)
                        .scale(0.1)
                        .toFile(ThumbnailImage);
            }
        }



    public static void saveUploadImageAndThumbnail(byte[] fileByte,String path, String fileName) throws IOException {

        File fileMkdir =new File(path);

        if  (!fileMkdir.exists()  && !fileMkdir.isDirectory())
        {
            fileMkdir.mkdirs();
        }
        String filePath=path+fileName;
        String FullImage = filePath+".jpg";
        String ThumbnailImage = filePath+"_t.jpg";

        InputStream stream1 = new ByteArrayInputStream(fileByte);
        InputStream stream2 = new ByteArrayInputStream(fileByte);

        Thumbnails.of(stream1)
                .scale(1)
                .toFile(FullImage);

        Thumbnails.of(stream2)
                .scale(0.1)
                .toFile(ThumbnailImage);
    }



    public static void saveUploadImage(HttpServletRequest request,String path,String fileName) throws IOException {
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext());

        if(multipartResolver.isMultipart(request))
        {

            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;

            Iterator iter=multiRequest.getFileNames();
            File fileMkdir =new File(path);

            if  (!fileMkdir.exists()  && !fileMkdir.isDirectory())
            {
                fileMkdir.mkdir();
            }
            while(iter.hasNext())
            {

                MultipartFile file=multiRequest.getFile(iter.next().toString());
                if(file!=null)
                {
                    String filePath=path+fileName;

                    file.transferTo(new File(filePath));
                    System.out.println("testPath:"+path);
                }
            }
        }
    }

}
